--躬行践履
local gongxingjianlv = fk.CreateSkill {
    name = "mcdh_gongxingjianlv"
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

gongxingjianlv:addEffect('viewas',{
    pattern = ".",
    card_filter = function(self,player, to_select, selected)
        return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeEquip
    end,
    interaction = function(self,player)
        local n = player:getMark("@gongxingjianlv-turn")
        local names = {}
        for _, id in ipairs(Fk:getAllCardIds()) do
            local card = Fk:getCardById(id)
            if (card.type == Card.TypeBasic or card:isCommonTrick()) and Fk:translate(card.trueName, "zh_CN"):len() == n and not card.is_derived  then
                local to_use = Fk:cloneCard(card.name)
                if ((Fk.currentResponsePattern == nil and card.skill:canUse(player, to_use) and not player:prohibitUse(to_use)) or
                        (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use))) then
                    table.insertIfNeed(names, card.name)
                end
            end
        end
        if #names == 0 then return  end
        return  U.CardNameBox { choices = names}
    end,
    view_as = function(self, player,cards)
        if #cards ~= 1 then return end
        if not self.interaction.data then return end
        local card
        card = Fk:cloneCard(self.interaction.data)
        card.skillName = self.name
        card:addSubcard(cards[1])
        return card
    end,
    enabled_at_play = function(self, player)
        if player:isNude() then
            return false
        end
        local n = player:getMark("@gongxingjianlv-turn")
        for _, id in ipairs(Fk:getAllCardIds()) do
            local card = Fk:getCardById(id)
            if (card.type == Card.TypeBasic or card:isCommonTrick()) and Fk:translate(card.trueName, "zh_CN"):len() == n and not card.is_derived  then
                local to_use = Fk:cloneCard(card.name)
                if ((Fk.currentResponsePattern == nil and card.skill:canUse(player, to_use) and not player:prohibitUse(to_use)) or
                        (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use))) then
                    return true
                end
            end
        end
    end,
    enabled_at_response = function (self, player, response)
        if  response then
            return false
        end
        local n = player:getMark("@gongxingjianlv-turn")
        for _, id in ipairs(Fk:getAllCardIds()) do
            local card = Fk:getCardById(id)
            if (card.type == Card.TypeBasic or card:isCommonTrick()) and Fk:translate(card.trueName, "zh_CN"):len() == n and not card.is_derived  then
                local to_use = Fk:cloneCard(card.name)
                if ((Fk.currentResponsePattern == nil and card.skill:canUse(player, to_use) and not player:prohibitUse(to_use)) or
                        (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use))) then
                    return true
                end
            end
        end
    end,
})

gongxingjianlv:addEffect(fk.AfterCardsMove,{
    can_refresh = function(self, event, target, player, data)
        if player:hasSkill(gongxingjianlv.name,true) then
            for _, move in ipairs(data) do
                if move.toArea == Card.DiscardPile  then
                    for _, info in ipairs(move.moveInfo) do
                        if Fk:getCardById(info.cardId).type ~= Card.TypeBasic then
                            return true
                        end
                    end
                end
            end
        end
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local n = 0
        for _, move in ipairs(data) do
            if move.toArea == Card.DiscardPile  then
                for _, info in ipairs(move.moveInfo) do
                    if Fk:getCardById(info.cardId).type ~= Card.TypeBasic then
                        n = n+1
                    end
                end
            end
        end
        room:addPlayerMark(player,"@gongxingjianlv-turn",n)
    end,
})

Fk:loadTranslationTable {
    ["mcdh_gongxingjianlv"] = "践行",
    ["@gongxingjianlv-turn"] = "躬行践履",
    [":mcdh_gongxingjianlv"] = "『躬行践履』<br>你可以将一张装备牌当做任意牌名字数为X的普通锦囊牌或基本牌使用(X为本回合进入弃牌堆的非基本牌数)。"
}
return gongxingjianlv